﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBBase;

namespace Business.系统设置
{
    public partial class Preview : Form
    {
        public Preview()
        {
            InitializeComponent();
        }
        public void getjilu()
        {
            lv.Items.Clear();

            Query query = new Query();
            string sql = "";

            sql = string.Format(@"select * from preview");

            DataTable dt = query.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        ListViewItem lt = new ListViewItem(string.Format("{0}", i + 1));
                        lt.SubItems.Add(dt.Rows[i]["preview_name"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["preview_type"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["preview_control"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["preview_mark"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["preview_sqlbs"].ToString().Trim());
                        this.lv.Items.Add(lt);
                    }
                }
            }
        }

        private void Preview_Load(object sender, EventArgs e)
        {
            inicbx(comboBox1, "table_2");
            getjilu();
        }

        void inicbx(ComboBox cbx, string dataname)
        {
            Query objquery = new Query();
            string ssqlex = "";
            ssqlex = string.Format(@"select distinct {0} from table_hpzl", dataname);
            DataTable dt = objquery.GetTable(ssqlex);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cbx.Items.Add(dt.Rows[i][dataname].ToString().Trim());
                if (i == 0)
                    cbx.Text = dt.Rows[0][dataname].ToString().Trim();
            }

        }

        private void skinButton3_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text.Length<1||textBox1.Text.Length<1)
            {
                 MessageBox.Show("预检项目名称和号牌类型不可都为空");
                return;
            }
            Query objquery = new Query();
            string ssql = "";
            ssql = string.Format(@"select * from preview where preview_type='{0}'and preview_name='{1}'", comboBox1.Text, textBox1.Text);
            DataTable dt = new DataTable();
            dt = objquery.GetTable(ssql);
            if (dt.Rows.Count > 0)
            {
                ssql = string.Format(@"update preview set preview_mark='{0}',preview_control='{1}',preview_sqlbs='{2}' where  preview_type='{3}' and preview_name='{4}'",
                                     text_neirong.Text, comboBox2.Text, textBox2.Text, comboBox1.Text, textBox1.Text);
            }
            else
            {
                ssql = string.Format(@"insert into preview (preview_type,preview_name,preview_mark,preview_control,preview_sqlbs) values('{0}','{1}','{2}','{3}','{4}')",
                                     comboBox1.Text, textBox1.Text, text_neirong.Text, comboBox2.Text, textBox2.Text);
            }
            objquery.InsertDB(ssql);
            getjilu();
        }

        private void skinButton4_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text.Length < 1 || textBox1.Text.Length < 1)
            {
                MessageBox.Show("预检项目名称和号牌类型不可都为空");
                return;
            }
            Query objquery = new Query();
            string ssql = "";
            ssql = string.Format(@"delete  preview where preview_type='{0}'and preview_name='{1}'", comboBox1.Text, textBox1.Text);
            objquery.InsertDB(ssql);
            getjilu();
        }

        private void lv_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListViewItem item in lv.SelectedItems)
            {
                textBox1.Text = item.SubItems[1].Text.ToString().Trim();
                comboBox1.Text = item.SubItems[2].Text.ToString().Trim();
                comboBox2.Text = item.SubItems[3].Text.ToString().Trim();
                text_neirong.Text = item.SubItems[4].Text.ToString().Trim();
                textBox2.Text = item.SubItems[5].Text.ToString().Trim();
            }
        }
    }
}
